package development.bean;

import state.font.FontManager;
import state.font.ImageText;
import development.ChatComponent;

public class ChatInfo {
  private int maxMessageLength;
  
  private String fromUser;
  
  private StringBuffer msg;
  
  public int timeCount = -1;
  
  private int messageLineCount = 0;
  
  private int lineMaxLength = 0;
  
  public ChatInfo(String fromUser1, int maxMessageLength1) {
    this.maxMessageLength = maxMessageLength1;
    this.fromUser = fromUser1;
    msg = new StringBuffer();
  }
  
  public int getLineMaxLength() {
    return lineMaxLength;
  }

  public String getFromUser() {
    return fromUser;
  }

  public String getMessage() {
    return msg.toString();
  }

  public void setMessage(String message) {
    msg.setLength(0);
    msg.append(message);
    messageLineCount = 1;
    lineMaxLength = getLengthOf(message);
  }
  
  public void appentMessage(String message) {
    if ((messageLineCount > 1) || (msg.length() + message.length() > maxMessageLength)) {
      msg.setLength(0);
      messageLineCount = 1;
      lineMaxLength = 0;
    } else {
      if (msg.length() > 0) {
        msg.append("\n");
        messageLineCount++;
      }
    }
    msg.append(message);
    lineMaxLength = Math.max(lineMaxLength, getLengthOf(message));
    lineMaxLength = Math.min(lineMaxLength, ChatComponent.MAX_CHAT_BOX_WIDTH);
  }
  
  private int getLengthOf(String text) {
    ImageText text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    return text8.stringWidth(text);
  }

  public int getMaxMessageLength() {
    return maxMessageLength;
  }

  public void setMaxMessageLength(int maxMessageLength1) {
    this.maxMessageLength = maxMessageLength1;
  }
  
  public void clear() {
    msg.setLength(0);
    messageLineCount = 0;
    lineMaxLength = 0;
  }
}
